<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('menus', function (Blueprint $table) {
            $table->id();
            $table->string('name', 100)->comment('菜单名称');
            $table->text('description')->nullable()->comment('菜单描述');
            $table->unsignedBigInteger('creator_id')->comment('创建者ID');
            $table->enum('source_type', ['default', 'custom'])->default('default')->comment('来源类型');
            $table->unsignedBigInteger('source_id')->nullable()->comment('来源ID（基于其他菜单创建）');
            $table->boolean('is_public')->default(false)->comment('是否公开');
            $table->boolean('is_template')->default(false)->comment('是否为模板菜单');
            $table->integer('usage_count')->default(0)->comment('使用次数');
            $table->decimal('rating', 3, 2)->default(0.00)->comment('评分');
            $table->timestamps();
            
            $table->foreign('creator_id')->references('id')->on('users')->onDelete('cascade');
            $table->foreign('source_id')->references('id')->on('menus')->onDelete('set null');
            
            $table->index('creator_id');
            $table->index('is_public');
            $table->index('is_template');
            $table->index(['source_type', 'source_id']);
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('menus');
    }
};